(function (vc, vm) {
    vc.extends({
        data: {
            addStaffPrivilegeInfo: {
                _currentUserId: '',
                name: '',
                description: '',
                errorInfo: '',
                _noAddPrivilege: [],
                _noAddPrivilegeGroup: [],
                _currentTab: 1,
                selectPrivileges: [],
                selectPrivilegeGroups: [],
                quanGroup: false,
                quan: false
            }
        },
        watch: { // 监视双向绑定的数据数组
            addStaffPrivilegeInfo: {
                handler() { // 数据数组有变化将触发此函数
                    if ($that.addStaffPrivilegeInfo.selectPrivileges.length == $that.addStaffPrivilegeInfo._noAddPrivilege.length) {
                        $that.addStaffPrivilegeInfo.quan = true;
                    } else {
                        $that.addStaffPrivilegeInfo.quan = false;
                    }
                    if ($that.addStaffPrivilegeInfo.selectPrivilegeGroups.length == $that.addStaffPrivilegeInfo._noAddPrivilegeGroup.length) {
                        $that.addStaffPrivilegeInfo.quanGroup = true;
                    } else {
                        $that.addStaffPrivilegeInfo.quanGroup = false;
                    }
                },
                deep: true // 深度监视
            }
        },
        _initMethod: function () {
        },
        _initEvent: function () {
            vc.on('addStaffPrivilege', 'addStaffPrivilegeModel', function (_params) {
                $('#addStaffPrivilegeModel').modal('show');
                $that._refreshData(_params);
            });
        },
        methods: {
            _refreshData: function (_params) {
                $that.addStaffPrivilegeInfo._currentUserId = _params.userId;
                $that.addStaffPrivilegeInfo._currentTab = 1;
                $that.listNoAddPrivilegeGroup();
            },
            changeTab: function (_tempTab) {
                $that.addStaffPrivilegeInfo._currentTab = _tempTab;
                if (_tempTab == 2) {
                    $that.listNoAddPrivilege();
                    return;
                }
                $that.listNoAddPrivilegeGroup();
            },
            listNoAddPrivilegeGroup: function () {
                $that.addStaffPrivilegeInfo._noAddPrivilegeGroup = [];
                var param = {
                    params: {
                        userId: $that.addStaffPrivilegeInfo._currentUserId
                    }
                };
                vc.http.get(
                    'addStaffPrivilege',
                    'listNoAddPrivilegeGroup',
                    param,
                    function (json, res) {
                        //vm.menus = vm.refreshMenuActive(JSON.parse(json),0);
                        if (res.status == 200) {
                            $that.addStaffPrivilegeInfo._noAddPrivilegeGroup = JSON.parse(json);
                            return;
                        }
                        $that.addStaffPrivilegeInfo.errorInfo = json;
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                        $that.addStaffPrivilegeInfo.errorInfo = errInfo;
                    });
            },
            listNoAddPrivilege: function () {
                $that.addStaffPrivilegeInfo._noAddPrivilege = [];
                var param = {
                    params: {
                        userId: $that.addStaffPrivilegeInfo._currentUserId
                    }
                }
                vc.http.get(
                    'addStaffPrivilege',
                    'listNoAddPrivilege',
                    param,
                    function (json, res) {
                        //vm.menus = vm.refreshMenuActive(JSON.parse(json),0);
                        if (res.status == 200) {
                            $that.addStaffPrivilegeInfo._noAddPrivilege = JSON.parse(json);
                            return;
                        }
                        $that.addStaffPrivilegeInfo.errorInfo = json;
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                        $that.addStaffPrivilegeInfo.errorInfo = errInfo;
                    });
            },
            addStaffPrivilege: function () {
                $that.addStaffPrivilegeInfo.errorInfo = "";
                var _pIds = [];
                var _selectPrivilegeGroups = $that.addStaffPrivilegeInfo.selectPrivilegeGroups;
                var _selectPrivileges = $that.addStaffPrivilegeInfo.selectPrivileges;
                if ($that.addStaffPrivilegeInfo._currentTab == 1) {
                    for (var _pIndex = 0; _pIndex < _selectPrivilegeGroups.length; _pIndex++) {
                        var _pgId = {
                            pId: _selectPrivilegeGroups[_pIndex]
                        }
                        _pIds.push(_pgId);
                    }
                } else {
                    for (var _pIndex = 0; _pIndex < _selectPrivileges.length; _pIndex++) {
                        var _pId = {
                            pId: _selectPrivileges[_pIndex]
                        }
                        _pIds.push(_pId);
                    }
                }
                if (_pIds.length < 1) {
                    vc.toast('未选择相应权限或权限组');
                    return;
                }
                var param = {
                    userId: $that.addStaffPrivilegeInfo._currentUserId,
                    pIds: _pIds,
                    pFlag: $that.addStaffPrivilegeInfo._currentTab
                };
                vc.http.post(
                    'addStaffPrivilege',
                    'addStaffPrivilegeOrPrivilegeGroup',
                    JSON.stringify(param),
                    {
                        emulateJSON: true
                    },
                    function (json, res) {
                        //vm.menus = vm.refreshMenuActive(JSON.parse(json),0);
                        if (res.status == 200) {
                            //关闭model
                            $('#addStaffPrivilegeModel').modal('hide');
                            vc.emit('staffPrivilege', '_loadStaffPrivileges', {
                                staffId: $that.addStaffPrivilegeInfo._currentUserId
                            });
                            return;
                        }
                        $that.addStaffPrivilegeInfo.errorInfo = json;
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                        $that.addStaffPrivilegeInfo.errorInfo = errInfo;
                    });
            },
            checkAll: function (e) {
                var checkObj = document.querySelectorAll('.checkItem'); // 获取所有checkbox项
                if (e.target.checked) { // 判定全选checkbox的勾选状态
                    for (var i = 0; i < checkObj.length; i++) {
                        if (!checkObj[i].checked) { // 将未勾选的checkbox选项push到绑定数组中
                            $that.addStaffPrivilegeInfo.selectPrivileges.push(checkObj[i].value);
                        }
                    }
                } else { // 如果是去掉全选则清空checkbox选项绑定数组
                    $that.addStaffPrivilegeInfo.selectPrivileges = [];
                }
            },
            checkAllGroup: function (e) {
                var checkObj = document.querySelectorAll('.checkGroupItem'); // 获取所有checkbox项
                if (e.target.checked) { // 判定全选checkbox的勾选状态
                    for (var i = 0; i < checkObj.length; i++) {
                        if (!checkObj[i].checked) { // 将未勾选的checkbox选项push到绑定数组中
                            $that.addStaffPrivilegeInfo.selectPrivilegeGroups.push(checkObj[i].value);
                        }
                    }
                } else { // 如果是去掉全选则清空checkbox选项绑定数组
                    $that.addStaffPrivilegeInfo.selectPrivilegeGroups = [];
                }
            }
        }
    });
})(window.vc, window.$that);